/**
 * 公共导航处理工具类
 * 统一处理项目导航逻辑，避免代码重复
 */
export class NavigationHelper {
  /**
   * 处理项目导航
   * @param {Object} item - 导航项目对象
   */
  static async navigateToItem(item) {
    try {
      // 显示加载提示
      uni.showLoading({
        title: '加载中...'
      });
      
      let targetUrl = '';
      let title = item.name;
      
      // 根据内容类型处理导航
      switch (item.content_type) {
        case 'markdown':
          if (item.content && item.content !== '*') {
            targetUrl = `/pages/markdown/markdown?content=${encodeURIComponent(item.content)}&title=${encodeURIComponent(title)}`;
          } else if (item.api_endpoint) {
            targetUrl = `/pages/markdown/markdown?endpoint=${encodeURIComponent(item.api_endpoint)}&title=${encodeURIComponent(title)}`;
          } else {
            this.showDevelopingToast(title);
            return;
          }
          break;
          
        case 'page':
          if (item.page_path) {
            targetUrl = item.page_path;
          } else {
            // 如果page_path为空，跳转到item-page显示页面项目关联关系
            targetUrl = `/pages/item-page/item-page?parent_id=${item.id}&title=${encodeURIComponent(title)}`;
          }
          break;
          
        case 'external':
          targetUrl = `/pages/webview/webview?url=${encodeURIComponent(item.external_url)}&title=${encodeURIComponent(title)}`;
          break;
          
        case 'subpage':
          targetUrl = `/pages/subpage/subpage?parent_id=${item.id}&title=${encodeURIComponent(title)}`;
          break;
          
        case 'component':
          targetUrl = `/pages/component-loader/component-loader?component=${encodeURIComponent(item.component_name)}&title=${encodeURIComponent(title)}`;
          break;
          
        case 'rich_text':
          targetUrl = `/pages/rich-text/rich-text?itemId=${item.id}&categoryId=${item.category_id || ''}&title=${encodeURIComponent(title)}`;
          break;
          
        case 'test_page':
          if (item.test_page) {
            targetUrl = `/pages/${item.test_page}/${item.test_page}?title=${encodeURIComponent(title)}`;
          } else {
            this.showDevelopingToast(title);
            return;
          }
          break;
          
        default:
          throw new Error(`不支持的内容类型: ${item.content_type}`);
      }
      
      if (targetUrl) {
        uni.navigateTo({
          url: targetUrl
        });
      } else {
        this.showDevelopingToast(title);
      }
      
      uni.hideLoading();
    } catch (error) {
      uni.hideLoading();
      // console.error('导航失败:', error);
      uni.showToast({
        title: error.message || '导航失败，请稍后再试',
        icon: 'none'
      });
    }
  }
  
  /**
   * 显示功能开发中提示
   * @param {string} title - 功能标题
   */
  static showDevelopingToast(title) {
    uni.showToast({
      title: `功能【${title}】开发中`,
      icon: 'none'
    });
    uni.hideLoading();
  }
}